package com.nj.dms.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.nj.dms.dao.UserDao;
import com.nj.dms.dao.impl.UserDaoImpl;

@WebServlet("/CheckUserName")
public class CheckUserName extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		PrintWriter out = response.getWriter();

		UserDao userDao = new UserDaoImpl();
		// 用户名是否存在的标记
		boolean userExistFlag = false;// 默认用户名不存在
		if (userDao.findUserByName(request.getParameter("userName"))) {// 返回为true 表明用户是合法用户
			userExistFlag = true;// 用户名存在
		}
		// 由服务器向客户端输出一个json对象字符串
		// json javascript object notation
		// {key1:value1,key2,value2,... }
		// {"flag":true}
		out.print("{\"flag\":" + userExistFlag + "}");
		out.close();// 调用close()方法，强制将内容输出到客户端

	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
